package com.flxz.user.service.impl;

import com.flxz.common.constant.CacheConstants;
import com.flxz.common.core.redis.RedisCache;
import com.flxz.user.service.IUserCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户分类Service业务层处理
 */
@Service
public class UserCategoryServiceImpl implements IUserCategoryService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<Map<String, Object>> getIncomeCategories() {
        // 构建缓存键
        String cacheKey = CacheConstants.USER_CATEGORY_KEY + "income";
        
        // 尝试从缓存获取
        List<Map<String, Object>> cachedCategories = redisCache.getCacheObject(cacheKey);
        if (cachedCategories != null) {
            return cachedCategories;
        }
        
        // 缓存未命中，从数据源获取
        List<Map<String, Object>> categories = new ArrayList<>();
        
        // 收入分类数据
        String[][] incomeData = {
            {"1", "工资"},
            {"2", "奖金"},
            {"3", "投资收益"},
            {"4", "意外之财"},
            {"5", "其他收入"}
        };
        
        for (String[] data : incomeData) {
            Map<String, Object> category = new HashMap<>();
            category.put("categoryId", Long.valueOf(data[0]));
            category.put("categoryName", data[1]);
            categories.add(category);
        }
        
        // 缓存结果，设置过期时间为24小时（分类数据相对稳定）
        redisCache.setCacheObject(cacheKey, categories, 24 * 3600, TimeUnit.SECONDS);
        
        return categories;
    }

    @Override
    public List<Map<String, Object>> getExpenseCategories() {
        // 构建缓存键
        String cacheKey = CacheConstants.USER_CATEGORY_KEY + "expense";
        
        // 尝试从缓存获取
        List<Map<String, Object>> cachedCategories = redisCache.getCacheObject(cacheKey);
        if (cachedCategories != null) {
            return cachedCategories;
        }
        
        // 缓存未命中，从数据源获取
        List<Map<String, Object>> categories = new ArrayList<>();
        
        // 支出分类数据
        String[][] expenseData = {
            {"1", "餐饮"},
            {"2", "交通"},
            {"3", "购物"},
            {"4", "娱乐"},
            {"5", "居住"},
            {"6", "医疗"},
            {"7", "教育"},
            {"8", "其他支出"}
        };
        
        for (String[] data : expenseData) {
            Map<String, Object> category = new HashMap<>();
            category.put("categoryId", Long.valueOf(data[0]));
            category.put("categoryName", data[1]);
            categories.add(category);
        }
        
        // 缓存结果，设置过期时间为24小时（分类数据相对稳定）
        redisCache.setCacheObject(cacheKey, categories, 24 * 3600, TimeUnit.SECONDS);
        
        return categories;
    }

    @Override
    public List<Map<String, Object>> getAllCategories() {
        // 构建缓存键
        String cacheKey = CacheConstants.USER_CATEGORY_KEY + "all";
        
        // 尝试从缓存获取
        List<Map<String, Object>> cachedCategories = redisCache.getCacheObject(cacheKey);
        if (cachedCategories != null) {
            return cachedCategories;
        }
        
        // 缓存未命中，从数据源获取
        List<Map<String, Object>> allCategories = new ArrayList<>();
        
        // 添加收入分类
        List<Map<String, Object>> incomeCategories = getIncomeCategories();
        for (Map<String, Object> category : incomeCategories) {
            Map<String, Object> newCategory = new HashMap<>(category);
            newCategory.put("type", "income");
            allCategories.add(newCategory);
        }
        
        // 添加支出分类
        List<Map<String, Object>> expenseCategories = getExpenseCategories();
        for (Map<String, Object> category : expenseCategories) {
            Map<String, Object> newCategory = new HashMap<>(category);
            newCategory.put("type", "expense");
            allCategories.add(newCategory);
        }
        
        // 缓存结果，设置过期时间为24小时（分类数据相对稳定）
        redisCache.setCacheObject(cacheKey, allCategories, 24 * 3600, TimeUnit.SECONDS);
        
        return allCategories;
    }
}
